<?php

/**
 * model_user
 * 用户类
 */

class Model_user extends YOU {

	public function __construct()
	{
		parent::__construct();
	}

	/** 添加用户 **/
	public function add($user_type, $data )
	{
        if(empty($data['start_time'])){
            $data['start_time'] = date("Y-m-d",time());
        }
        if(empty($data['end_time'])){
            $data['end_time'] = "2050-01-01";
        }
		$start_time = strtotime($data['start_time']);
		$end_time   = strtotime($data['end_time']);

		switch ($user_type){

			case "admin":

				$info = array(
				"user_name"     => $data['user_name'],
				"user_type"     => "admin",
				"type_id"       => 0,
				"user_group"    => $data['user_group'],
				"user_pms"      => $data['user_pms'],
				"status"        => 1,
				"start_time"    => $start_time,
				"end_time"      => $end_time,
				"create_time"   => date("Y-m-d H:i:s"),
				"update_time"   => "0000-00-00 00:00:00"
				);
				$this->db->do_insert("tbl_user", $info);
				$user_id = $this->db->insert_id();


				break;

			case "repair":

				$info = array(
				"user_name"     => $data['user_name'],
				"user_type"     => "repair",
				"type_id"       => 0,
				"user_group"    => $data['user_group'],
				"user_pms"      => $data['user_pms'],
				"status"        => 1,
				"start_time"    => $start_time,
				"end_time"      => $end_time,
				"create_time"   => date("Y-m-d H:i:s"),
				"update_time"   => "0000-00-00 00:00:00"
				);
				$this->db->do_insert("tbl_user", $info);
				$user_id = $this->db->insert_id();


				break;

			case "staff":
				//echo $data['staff_fix'];exit;
				$data['staff_number'] = ltrim($data['staff_number']);

				$info = array(
				"staff_name"       => $data['staff_name'],
				"staff_number"     => $data['staff_number'],
				"staff_sex"        => $data['staff_sex'],
				"staff_position"   => $data['staff_position'],
				"staff_education"  => $data['staff_education'],
				"staff_party"      => $data['staff_party'],
				"staff_birth"      => $data['staff_birth'],
				"staff_card"       => $data['staff_card'],
				"staff_entry"      => $data['staff_entry'],
				"staff_img"        => $data['staff_img'],
				"staff_file"       => $data['staff_file'],
				"staff_external"   => $data['staff_external'],
				"start_time"       => $data['start_time'],
				"staff_tel"        => $data['staff_tel'],
				"end_time"   	   => $data['end_time'],
				"staff_nature"     => $data['staff_nature'],
				"staff_classes"     => $data['staff_classes'],
				"staff_zhuang"     => $data['staff_zhuang'],
				"status"           => 1,
				"create_time"      => date("Y-m-d H:i:s"),
				"update_time"      => "0000-00-00 00:00:00",
				"staff_usedname"   => $data['staff_usedname'],
				"staff_area"       => $data['staff_area'],
				"staff_idtype"     => $data['staff_idtype'],
				"staff_home"       => $data['staff_home'],
				"staff_city"       => $data['staff_city'],
				"staff_nation"     => $data['staff_nation'],
				"staff_marry"      => $data['staff_marry'],
				"staff_health"     => $data['staff_health'],
				"staff_orignal"    => $data['staff_orignal'],
				"staff_style"      => $data['staff_style'],
				"staff_signed"     => $data['staff_signed'],
				"staff_normal"     => $data['staff_normal'],
				"staff_skill"      => $data['staff_skill'],
				"staff_certificate"=> $data['staff_certificate'],
				"staff_free"       => $data['staff_free'],
				"staff_info"       => $data['staff_info'],
				"staff_basic"      => $data['staff_basic'],
				"staff_basic_start"=> $data['staff_basic_start'],
				"staff_basic_end"  => $data['staff_basic_end'],
				"staff_super"      => $data['staff_super'],
				"staff_bone"       => $data['staff_bone'],
				"staff_mental"     => $data['staff_mental'],
				"staff_sign"       => $data['staff_sign'],
				"staff_ctime"      => $data['staff_ctime'],
				"staff_party_date" => $data['staff_party_date'],
				"staff_addr"       => $data['staff_addr'],
				"staff_fix"        => ' '.$data['staff_fix'],
				"staff_educate"    => $data['staff_educate'],
				"staff_school"     => $data['staff_school'],
				"staff_graduate_time"=> $data['staff_graduate_time'],
				"staff_remark"     => $data['staff_remark'],

				);


				$this->db->do_insert("tbl_staff", $info);
				$staff_id = $this->db->insert_id();


				$info = array(
				"user_name"     => $data['staff_number'],
				"user_type"     => "staff",
				"type_id"       => $staff_id,
				"user_group"    => $data['user_group'],
				"user_pms"      => $data['user_pms'],
				"status"        => 1,
				"start_time"    => $start_time,
				"end_time"      => $end_time,
				"create_time"   => date("Y-m-d H:i:s"),
				"update_time"   => "0000-00-00 00:00:00"
				);
				$this->db->do_insert("tbl_user", $info);
				$user_id = $this->db->insert_id();


				break;

			case "student":
				$data['student_no'] = ltrim($data['student_no']);
				$info = array(
				"student_name"      	=> $data['student_name'],
				"student_sex"			=> $data['student_sex'],
				"student_birth"      	=> $data['student_birth'],
				"student_card"      	=> $data['student_card'],
				"student_nation"      	=> $data['student_nation'],
				"student_registered"    => $data['student_registered'],
				"student_tel"			=> $data['student_tel'],
				"student_no"			=> $data['student_no'],
				"student_source"      	=> $data['student_source'],
				"student_school"      	=> $data['student_school'],
				"student_variation"     => $data['student_variation'],
				"student_mode"      	=> $data['student_mode'],
				"student_classes"      	=> $data['student_classes'],
				"student_disability"    => $data['student_disability'],
				"status"           		=> 1,
				"create_time"      		=> date("Y-m-d H:i:s"),
				"update_time"      		=> "0000-00-00 00:00:00",
				"student_office"      	=> $data['student_office'],
				"student_register"		=> $data['student_register'],
				"student_over"      	=> $data['student_over'],
				"student_islocal"      	=> $data['student_islocal'],
				"student_native"      	=> $data['student_native'],
				"student_ctime"    		=> $data['student_ctime'],
				"student_remarks"		=> $data['student_remarks'],
				"student_addr"			=> $data['student_addr'],
				"student_snumber"      	=> $data['student_snumber'],
				"student_sname"      	=> $data['student_sname'],
				"student_city"     		=> $data['student_city'],
				"student_country"      	=> $data['student_country'],
				"student_idtype"      	=> $data['student_idtype'],
				"grade_id"    			=> $data['grade_id'],
				"student_china"      	=> $data['student_china'],
				"class_id"				=> $data['class_id'],
				"student_politics"      => $data['student_politics'],
				"student_health"      	=> $data['student_health'],
				"student_register_place"=> $data['student_register_place'],
				"student_now_addr"    	=> $data['student_now_addr'],
				"student_start_time"	=> $data['student_start_time'],
				"student_way"			=> $data['student_way'],
				"student_tel_addr"      => $data['student_tel_addr'],
				"student_home_addr"     => $data['student_home_addr'],
				"student_mail"     		=> $data['student_mail'],
				"is_work"      			=> $data['is_work'],
				"is_alone"      		=> $data['is_alone'],
				"is_study"    			=> $data['is_study'],
				"is_stay"      			=> $data['is_stay'],
				"is_help"				=> $data['is_help'],
				"is_martyr"      		=> $data['is_martyr'],
				"is_subsidy"      		=> $data['is_subsidy'],
				"student_help_no"      	=> $data['student_help_no'],
				"is_left"    			=> $data['is_left'],
				"student_class_no"		=> $data['student_class_no'],
				"student_from"			=> $data['student_from'],
				"student_read"      	=> $data['student_read'],
				"student_disability"    => $data['student_disability'],
				"student_parent"     	=> $data['student_parent'],
				"student_home_relation" => $data['student_home_relation'],
				"student_home_work"     => $data['student_home_work'],
				"student_parent_addr"   => $data['student_parent_addr'],
				"student_ptel"      	=> $data['student_ptel'],
				"student_parent_city"   => $data['student_parent_city'],


				);


				$this->db->do_insert("tbl_student", $info);
				$student_id = $this->db->insert_id();
				$info = array(
				"user_name"     => $data['student_no'],
				"user_type"     => "student",
				"type_id"       => $student_id,
				"user_group"    => $data['user_group'],
				"user_pms"      => $data['user_pms'],
				"status"        => 1,
				"start_time"    => $start_time,
				"end_time"      => $end_time,
				"create_time"   => date("Y-m-d H:i:s"),
				"update_time"   => "0000-00-00 00:00:00"
				);
				$this->db->do_insert("tbl_user", $info);
				$user_id = $this->db->insert_id();

				break;

		}

		// 初始密码
		$user_password = $this->getPass($user_id);
		$user_password = md5($user_password);
		$info = array(
		"user_password" => $user_password,
		);
		$this->db->do_update("tbl_user", $info, "user_id='{$user_id}'");

		return $user_id;
	}


	/** 读取信息 **/
	public function getInfo($user_id, $field = "*")
	{
		$data = $this->db->fetchRow("SELECT {$field} FROM `tbl_user` WHERE user_id='{$user_id}' ");
		return $data;
	}

	/** 读取姓名 **/
	public function getNameById($user_id)
	{

		$user_data = $this->getInfo($user_id);

		if ($user_data['user_type'] == "staff"){
			$row = $this->db->fetchRow("SELECT staff_name FROM `tbl_staff` WHERE staff_id='{$user_data['type_id']}'");

			return $row['staff_name'];
		}
		if ($user_data['user_type'] == "admin"){
			$row = $this->getInfo($user_id, "user_name");

			return $row['user_name'];
		}

		if ($user_data['user_type'] == "student"){
			$row = $this->db->fetchRow("SELECT student_name FROM `tbl_student` WHERE student_id='{$user_data['type_id']}'");

			return $row['student_name'];
		}

	}
	/** 读取名字获取user_id **/
	public function getIdByName($user_name, $user_type = "staff")
	{
	
		switch ($user_type){
			case "staff":
				$row  = $this->db->fetchRow("SELECT staff_id FROM `tbl_staff` WHERE staff_name='{$user_name}'");
				$data = $this->db->fetchRow("SELECT user_id FROM `tbl_user` WHERE type_id='{$row['staff_id']}' AND user_type='staff'");
				$user_id = $data['user_id'];
				break;
			case "student":
				$row  = $this->db->fetchRow("SELECT student_id FROM `tbl_student` WHERE student_name='{$user_name}'");
				$data = $this->db->fetchRow("SELECT user_id FROM `tbl_user` WHERE type_id='{$row['staff_id']}' AND user_type='student' ");
				$user_id = $data['user_id'];
				break;
			case "admin":
				$data = $this->db->fetchRow("SELECT user_id FROM `tbl_user` WHERE user_name='{$user_name}' AND user_type='admin' ");
				$user_id = $data['user_id'];
				break;
		}

		return $user_id;
	}
	/** 通过模糊学生名字获取user_id **/
	public function getIdByMName($user_name)
	{

		$data = $this->db->fetchRow("SELECT student_id FROM `tbl_student` WHERE student_name LIKE '%{$user_name}%'");

		$data = $this->db->fetchRow("SELECT user_id FROM `tbl_user` WHERE type_id='{$data['student_id']}' AND user_type='student' ");

		return $data['user_id'];
	}
	/** 读取姓名二维数组 **/
	public function getNameByIdTo($user_id)
	{
		$user_data = $this->getInfo($user_id);

		if ($user_data['user_type'] == "staff"){
			$row = $this->db->fetchRow("SELECT staff_name FROM `tbl_staff` WHERE staff_id='{$user_data['type_id']}'");

			return $row;
		}
		if ($user_data['user_type'] == "admin"){
			$row = $this->getInfo($user_id, "user_name");

			return $row;
		}

		if ($user_data['user_type'] == "student"){
			$row = $this->db->fetchRow("SELECT student_name FROM `tbl_student` WHERE student_id='{$user_data['type_id']}'");

			return $row;
		}

	}

	/** 读取id根据TYPE——id **/
	public function getIdByType($user_type, $type_id)
	{
		$user_data = $this->db->fetchRow("SELECT user_id FROM `tbl_user` WHERE user_type='{$user_type}' AND type_id='{$type_id}'");
		return $user_data['user_id'];
	}

	/** 获得初始密码 **/
	public function getPass($user_id)
	{
		$user_data = $this->getInfo($user_id);
		if (empty($user_data)){
			return false;
		}

		switch ($user_data['user_type']){
			case "admin":
				$user_password = "111111";
				break;
			case "repair":
				$user_password = "666666";
				break;
			case "staff":
				$staff_data = $this->db->fetchRow("SELECT staff_birth FROM `tbl_staff` WHERE staff_id='{$user_data['type_id']}'");
				$staff_birth = date("ymd", strtotime($staff_data['staff_birth']));

				$user_password = $staff_birth;
				break;

			case "student":
				$student_data = $this->db->fetchRow("SELECT student_birth FROM `tbl_student` WHERE student_id='{$user_data['type_id']}'");
				$student_birth = date("ymd", strtotime($student_data['student_birth']));

				$user_password = $student_birth;
				break;
		}

		return $user_password;


	}
	/** 根据id删除 **/
	public function getDel($type_id, $user_type, $field = "*")
	{

		$data = $this->db->query("DELETE  FROM `tbl_user` WHERE type_id='{$type_id}' AND user_type='{$user_type}'");

		return $data;
	}

	/** 操作记录 **/
	public function log()
	{

		// 用户

		// get参数

		// post参数



	}

}

?>